package com.vader.admin.service.impl;

import com.vader.admin.client.DistributeIdClient;
import com.vader.admin.dao.BaseIntegrationRecordDao;
import com.vader.admin.dao.BaseUserIntegrationDao;
import com.vader.admin.entity.DO.BaseIntegrationRecord;
import com.vader.admin.entity.DO.BaseUserIntegration;
import com.vader.admin.entity.Query.EditIntegrationQuery;
import com.vader.admin.service.BaseIntegrationService;
import com.vader.common.exception.BadRequestException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * @author Vader
 * @date 2021/10/8
 **/
public class BaseIntegrationServiceImpl implements BaseIntegrationService {
    @Resource
    private BaseUserIntegrationDao baseUserIntegrationDao;
    @Resource
    private BaseIntegrationRecordDao baseIntegrationRecordDao;
    @Resource
    private DistributeIdClient distributeIdClient;

    /**
     * 通过ID查询单条数据
     *
     * @param uid 主键
     * @return 实例对象
     */
    @Override
    public BaseUserIntegration queryById(Long uid) {
        return this.baseUserIntegrationDao.queryById(uid);
    }

    /**
     * 分页查询
     *
     * @param baseUserIntegration 筛选条件
     * @param pageRequest         分页对象
     * @return 查询结果
     */
    @Override
    public Page<BaseUserIntegration> queryByPage(BaseUserIntegration baseUserIntegration, PageRequest pageRequest) {
        long total = this.baseUserIntegrationDao.count(baseUserIntegration);
        return new PageImpl<>(this.baseUserIntegrationDao.queryAllByLimit(baseUserIntegration, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param baseUserIntegration 实例对象
     * @return 实例对象
     */
    @Override
    public BaseUserIntegration insert(BaseUserIntegration baseUserIntegration) {
        this.baseUserIntegrationDao.insert(baseUserIntegration);
        return baseUserIntegration;
    }

    /**
     * 积分变更
     *
     * @param editIntegrationQuery 实例对象
     * @return 实例对象
     */
    @Override
    public BaseUserIntegration update(EditIntegrationQuery editIntegrationQuery) {
        BaseIntegrationRecord baseIntegrationRecord = BaseIntegrationRecord.from(editIntegrationQuery);
        baseIntegrationRecord.setId(distributeIdClient.getId().getData());
        int insert = baseIntegrationRecordDao.insert(baseIntegrationRecord);
        if (insert < 1) {
            throw new BadRequestException("生成积分记录出错");
        }
        if (editIntegrationQuery.getSendType() != 0) {
            BaseUserIntegration sendUserIntegration = baseUserIntegrationDao.queryById(editIntegrationQuery.getSendUid());
            Integer integraionType = editIntegrationQuery.getIntegraionType();
            switch (integraionType) {
                case 0:
                    Long integration = sendUserIntegration.getIntegration();
                    integration = integration - editIntegrationQuery.getSum();
                    sendUserIntegration.setIntegration(integration);
                    break;
                case 1:
                    Integer pearlFragment = sendUserIntegration.getPearlFragment();
                    pearlFragment = pearlFragment - editIntegrationQuery.getSum();
                    sendUserIntegration.setPearlFragment(pearlFragment);
                    break;
                case 2:
                    Integer giftPearlFragment = sendUserIntegration.getGiftPearlFragment();
                    giftPearlFragment = giftPearlFragment - editIntegrationQuery.getSum();
                    sendUserIntegration.setGiftPearlFragment(giftPearlFragment);
                    break;
                case 3:
                    Integer dragonPearl = sendUserIntegration.getDragonPearl();
                    dragonPearl = dragonPearl - editIntegrationQuery.getSum();
                    sendUserIntegration.setDragonPearl(dragonPearl);
                    break;
                default:
                    throw new BadRequestException("积分类型出错");
            }
            baseUserIntegrationDao.update(sendUserIntegration);
        }

        BaseUserIntegration reciveUserIntegration = baseUserIntegrationDao.queryById(editIntegrationQuery.getReceiveUid());
        Integer integraionType = editIntegrationQuery.getIntegraionType();
        switch (integraionType) {
            case 0:
                Long integration = reciveUserIntegration.getIntegration();
                integration = integration + editIntegrationQuery.getSum();
                reciveUserIntegration.setIntegration(integration);
                break;
            case 1:
                Integer pearlFragment = reciveUserIntegration.getPearlFragment();
                pearlFragment = pearlFragment + editIntegrationQuery.getSum();
                if (pearlFragment >=3) {
                    pearlFragment = pearlFragment % 3;
                    Integer dragonPearl = reciveUserIntegration.getDragonPearl();
                    dragonPearl = dragonPearl + pearlFragment / 3;
                    reciveUserIntegration.setDragonPearl(dragonPearl);
                }
                reciveUserIntegration.setPearlFragment(pearlFragment);
                break;
            case 2:
                Integer giftPearlFragment = reciveUserIntegration.getGiftPearlFragment();
                giftPearlFragment = giftPearlFragment + editIntegrationQuery.getSum();
                reciveUserIntegration.setGiftPearlFragment(giftPearlFragment);
                break;
            case 3:
                Integer dragonPearl = reciveUserIntegration.getDragonPearl();
                dragonPearl = dragonPearl + editIntegrationQuery.getSum();
                reciveUserIntegration.setDragonPearl(dragonPearl);
                break;
            default:
                throw new BadRequestException("积分类型出错");
        }
        baseUserIntegrationDao.update(reciveUserIntegration);
        return baseUserIntegrationDao.queryById(reciveUserIntegration.getUid());
    }

    /**
     * 通过主键删除数据
     *
     * @param uid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long uid) {
        return this.baseUserIntegrationDao.deleteById(uid) > 0;
    }

}
